#!/bin/bash
 
tar xzf curl-7.88.0.tar.xz
 
source ./build_env.sh
 
INSTALL_DIR=$BUILD_DIR/curl
 
if [ ! -d $INSTALL_DIR ]; then
    mkdir -p $INSTALL_DIR
fi
 
cd curl-7.88.0
 
function build_device() {
    make distclean
    
    ARCH=$1
    PLATFORM="iphoneos"
    SDK_PATH=$(xcrun -sdk $PLATFORM --show-sdk-path)
    
    export CC=$(xcrun -sdk $PLATFORM -find clang)
    export CFLAGS="-arch $ARCH -isysroot $SDK_PATH -miphoneos-version-min=$IPHONEOS_DEPLOYMENT_TARGET -I$SDK_PATH/usr/include"
    export CPPFLAGS="-I$SDK_PATH/usr/include"
    export AR=$(xcrun -sdk $PLATFORM -find ar)
    export RANLIB=$(xcrun -sdk $PLATFORM -find ranlib)
    export LDFLAGS="-arch $ARCH -isysroot $SDK_PATH"
    
    ./configure --host=arm-apple-darwin \
                --prefix=$INSTALL_DIR/$ARCH \
                --with-zlib=$BUILD_DIR/zlib/$ARCH \
                --with-ssl=$BUILD_DIR/openssl/$ARCH \
                --enable-static \
                --disable-shared \
                --enable-nonblocking \
                --disable-verbose \
                --disable-dependency-tracking \
                --disable-ftp \
                --disable-file \
                --disable-ldap \
                --disable-ldaps \
                --disable-rtsp \
                --disable-proxy \
                --disable-dict \
                --disable-telnet \
                --disable-tftp \
                --disable-pop3 \
                --disable-imap \
                --disable-smtp \
                --disable-gopher \
                --disable-sspi \
                --enable-ipv6 \
                --disable-manual \
                ac_cv_func_fcntl_o_nonblock=yes \
                ac_cv_func_ioctlsocket_fionbio=no \
                ac_cv_func_setsockopt_so_nonblock=no
    
    make -j8
    make install
    make distclean
}

function build_simulator() {
    make distclean
    
    ARCH=$1
    PLATFORM="iphonesimulator"
    SDK_PATH=$(xcrun -sdk $PLATFORM --show-sdk-path)
    
    export CC=$(xcrun -sdk $PLATFORM -find clang)
    export CFLAGS="-arch $ARCH -isysroot $SDK_PATH -miphonesimulator-version-min=$IPHONEOS_DEPLOYMENT_TARGET -I$SDK_PATH/usr/include"
    export CPPFLAGS="-I$SDK_PATH/usr/include"
    export AR=$(xcrun -sdk $PLATFORM -find ar)
    export RANLIB=$(xcrun -sdk $PLATFORM -find ranlib)
    export LDFLAGS="-arch $ARCH -isysroot $SDK_PATH"
    
    ./configure --host=x86_64-apple-darwin \
                --prefix=$INSTALL_DIR/$ARCH \
                --with-zlib=$BUILD_DIR/zlib/$ARCH \
                --with-ssl=$BUILD_DIR/openssl/$ARCH \
                --enable-static \
                --disable-shared \
                --enable-nonblocking \
                --disable-verbose \
                --disable-dependency-tracking \
                --disable-ftp \
                --disable-file \
                --disable-ldap \
                --disable-ldaps \
                --disable-rtsp \
                --disable-proxy \
                --disable-dict \
                --disable-telnet \
                --disable-tftp \
                --disable-pop3 \
                --disable-imap \
                --disable-smtp \
                --disable-gopher \
                --disable-sspi \
                --enable-ipv6 \
                --disable-manual \
                ac_cv_func_fcntl_o_nonblock=yes \
                ac_cv_func_ioctlsocket_fionbio=no \
                ac_cv_func_setsockopt_so_nonblock=no
    
    make -j8
    make install
    make distclean
}

# 编译各个架构 - 只编译arm64和x86_64
build_device arm64
build_simulator x86_64

# 创建通用库
UNIVERSAL_DIR=$INSTALL_DIR/universal
mkdir -p $UNIVERSAL_DIR/lib
mkdir -p $UNIVERSAL_DIR/include

cp -R $INSTALL_DIR/arm64/include/* $UNIVERSAL_DIR/include/

# 合并所有架构的库文件
lipo -create \
    $INSTALL_DIR/arm64/lib/libcurl.a \
    $INSTALL_DIR/x86_64/lib/libcurl.a \
    -output $UNIVERSAL_DIR/lib/libcurl.a
 
cd ..
 
rm -rf curl-7.88.0